<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" size="small" :inline="true">
      <el-form-item label="会话ID" prop="sessionId">
        <el-input v-model="queryParams.sessionId" placeholder="请输入会话ID" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="设备ID" prop="deviceId">
        <el-input v-model="queryParams.deviceId" placeholder="请输入设备ID" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="MAC地址" prop="macAddress">
        <el-input v-model="queryParams.macAddress" placeholder="请输入MAC地址" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="来源" prop="src">
        <el-input v-model="queryParams.src" placeholder="请输入来源" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="状态" clearable>
          <el-option label="失效" :value="0" />
          <el-option label="有效" :value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd HH:mm:ss"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['wifi:guide:remove']"
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="guidePageAccessList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="会话ID" align="center" prop="sessionId" min-width="200" />
      <el-table-column label="设备ID" align="center" prop="deviceId" min-width="150" />
      <el-table-column label="MAC地址" align="center" prop="macAddress" min-width="140" />
      <el-table-column label="来源" align="center" prop="src" width="80" />
      <el-table-column label="渠道ID" align="center" prop="channelId" width="80" />
      <el-table-column label="厂商时间戳" align="center" prop="vendorTimestamp" width="120">
        <template slot-scope="scope">
          <span v-if="scope.row.vendorTimestamp">{{ parseTime(scope.row.vendorTimestamp) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="签名版本" align="center" prop="signVer" width="80" />
      <el-table-column label="用户IP" align="center" prop="userIp" width="120" />
      <el-table-column label="状态" align="center" prop="status" width="80">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 1" type="success">有效</el-tag>
          <el-tag v-else type="danger">失效</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="160" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
          >查看详情</el-button>
          <el-button
            v-hasPermi="['wifi:guide:remove']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 详情对话框 -->
    <el-dialog title="引导页访问详情" :visible.sync="open" width="800px" append-to-body>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="会话ID">{{ form.sessionId }}</el-descriptions-item>
        <el-descriptions-item label="设备ID">{{ form.deviceId }}</el-descriptions-item>
        <el-descriptions-item label="MAC地址">{{ form.macAddress }}</el-descriptions-item>
        <el-descriptions-item label="来源">{{ form.src }}</el-descriptions-item>
        <el-descriptions-item label="渠道ID">{{ form.channelId }}</el-descriptions-item>
        <el-descriptions-item label="厂商时间戳">{{ form.vendorTimestamp ? parseTime(form.vendorTimestamp) : '' }}</el-descriptions-item>
        <el-descriptions-item label="签名">{{ form.sign }}</el-descriptions-item>
        <el-descriptions-item label="签名版本">{{ form.signVer }}</el-descriptions-item>
        <el-descriptions-item label="用户IP">{{ form.userIp }}</el-descriptions-item>
        <el-descriptions-item label="用户代理" :span="2">{{ form.userAgent }}</el-descriptions-item>
        <el-descriptions-item label="来源页面" :span="2">{{ form.referer }}</el-descriptions-item>
        <el-descriptions-item label="状态">
          <el-tag v-if="form.status === 1" type="success">有效</el-tag>
          <el-tag v-else type="danger">失效</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ form.createTime }}</el-descriptions-item>
        <el-descriptions-item label="更新时间">{{ form.updateTime }}</el-descriptions-item>
      </el-descriptions>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listGuidePageAccess, getGuidePageInfo } from '@/api/wifi/guide'

export default {
  name: 'GuidePageAccess',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 引导页访问记录表格数据
      guidePageAccessList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 表单参数
      form: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sessionId: null,
        src: null,
        macAddress: null,
        deviceId: null,
        channelId: null,
        vendorTimestamp: null,
        sign: null,
        signVer: null,
        userIp: null,
        status: null
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询引导页访问记录列表 */
    getList() {
      this.loading = true
      listGuidePageAccess(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.guidePageAccessList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {}
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 查看详情按钮操作 */
    handleView(row) {
      this.reset()
      const id = row.id
      getGuidePageInfo(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '引导页访问详情'
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除引导页访问记录编号为"' + ids + '"的数据项？').then(function() {
        // 这里需要添加删除API调用
        // return delGuidePageAccess(ids);
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {})
    }
  }
}
</script>
